package com.solution.admin.product.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.solution.admin.product.dao.ProductCategoryPropertyFilterDao;
import com.solution.admin.product.dao.ProductDao;
import com.solution.admin.product.entity.ProductCategoryPropertyFilterEntity;
import com.solution.admin.product.entity.ProductEntity;
import com.solution.admin.product.service.ProductCategoryPropertyFilterService;
import com.solution.admin.settings.dao.SortDao;
import com.solution.common.dao.BaseDao;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.service.impl.BaseServiceImpl;
import com.solution.common.utils.string.StringUtil;

/**
 * 商品分类搜索筛选项service实现层
 * @author lilp
 * @date 2017-10-09 23:17:28
 */
@Service
public class ProductCategoryPropertyFilterServiceImpl extends BaseServiceImpl<ProductCategoryPropertyFilterEntity> implements ProductCategoryPropertyFilterService {
	@Autowired
	private SortDao sortDao;
	/**
	 * 商品分类搜索筛选项接口
	 */
	@Autowired
	private ProductCategoryPropertyFilterDao productCategoryPropertyFilterDao;
	
	@Autowired
	private ProductDao productDao;

	protected BaseDao<ProductCategoryPropertyFilterEntity> getDao() {
		return this.productCategoryPropertyFilterDao;
	}

	/**
	 * 保存商品分类搜索筛选项
	 * @param productCategoryPropertyFilterEntity 商品分类搜索筛选项实体
	 * @return Long 添加成功商品分类搜索筛选项的ID
	 * @author lilp
	 * @date 2017-10-09 23:17:28
	 */
	public long saveProductCategoryPropertyFilter(ProductCategoryPropertyFilterEntity productCategoryPropertyFilterEntity) {
//		productCategoryPropertyFilterEntity.setSortNo(sortDao.getMaxSortNo("eb_product_category_property_filter") + 1);
		productCategoryPropertyFilterEntity.setSortNo(productCategoryPropertyFilterDao.getMaxSortNo(productCategoryPropertyFilterEntity.getProductCategoryId())+1);
		return productCategoryPropertyFilterDao.saveProductCategoryPropertyFilter(productCategoryPropertyFilterEntity);
	}

	/**
	 * 分页查询商品分类搜索筛选项
	 * @param pageParam 分页参数
	 * @param paramMap 查询参数
	 * @return PageBean 分页参数和分页数据
	 * @author lilp
	 * @date 2017-10-09 23:17:28
	 */
	@Override
	public PageBean listProductCategoryPropertyFilterPage(PageParam pageParam, Map<String, Object> paramMap) {
		return productCategoryPropertyFilterDao.listProductCategoryPropertyFilterPage(pageParam, paramMap);
	}
	
	/**
	 * 根据ID查询用户信息
	 * @param id 用户id
	 * @return productCategoryPropertyFilterEntity 用户对象
	 * @author lilp
	 * @date 2017-10-09 23:17:28
	 */
	public ProductCategoryPropertyFilterEntity getProductCategoryPropertyFilterById(Long id) {
		return productCategoryPropertyFilterDao.getById(id.longValue());
	}

	/**
	 * 更新用户
	 * @param productCategoryPropertyFilterEntity 用户实体
	 * @return Long 更新是否成功，>1代表更新成功
	 * @author lilp
	 * @date 2017-10-09 23:17:28
	 */
	@Override
	public int updateProductCategoryPropertyFilter(ProductCategoryPropertyFilterEntity productCategoryPropertyFilterEntity) {
		return productCategoryPropertyFilterDao.updateProductCategoryPropertyFilter(productCategoryPropertyFilterEntity);
	}
	
	
	/**
	 * 根据条件查询商品分类搜索筛选项
	 * @param param
	 * @return 数据列表
	 * @author lilp
	 * @date 2017-10-09 23:17:28
	 */
	@Override
	public List<ProductCategoryPropertyFilterEntity> listProductCategoryPropertyFilter( Map<String, Object> param){
		return  productCategoryPropertyFilterDao.listProductCategoryPropertyFilter(param);
	}
	
	/**
	 * 删除商品分类搜索筛选项
	 * @param 
	 * @return int 返回删除记录条数
	 * @author lilp
	 * @date 2017-10-09 23:17:28
	 */
	public int deleteProductCategoryPropertyFilterById(long id){
		return  productCategoryPropertyFilterDao.deleteById(id);
	}
	
	
	/**
	 * 查询商品列表页面，筛选项
	 * @param param
	 * @return
	 */
	@Override
	public List<ProductCategoryPropertyFilterEntity> listProductCategoryPropertyFilterForIndex(Map<String, Object> param){
		String serachKey = (String)param.get("searchKey");
		if(StringUtil.isNotEmpty(serachKey)){
			// 查询商品关联的类型及像级类型
			List<ProductEntity> productList = productDao.listRelatedProductCategory(param);
			if(productList != null && productList.size() > 0){
				StringBuffer tmpBuf = new StringBuffer();
				for (ProductEntity category : productList) {
					String path = category.getProductCategoryName();
					if(StringUtil.isNotEmpty(path)){
						String[] tmpArr = path.split("\\|");
						for (String id : tmpArr) {
							tmpBuf.append(id).append(",");
						}
					}
					if(tmpBuf.length() > 0){
						String resultIds = tmpBuf.toString().substring(0,tmpBuf.length()-1);
						param.put("categoryIds", "("+resultIds+")");
					}
				}
			}
		}
		return productCategoryPropertyFilterDao.listProductCategoryPropertyFilterForIndex(param);
		
	}
	
}
